package com.caiya.controller;

import com.caiya.cart.Res;
import com.caiya.cart.dto.order.CartOrderListDto;
import com.caiya.cart.vo.order.CartOrderAddVo;
import com.caiya.cart.vo.order.CartOrderListVo;
import com.caiya.cart.vo.order.CartOrderUpdateVo;
import com.caiya.service.CartOrderService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by Administrator on 2018/12/5 0005.
 */
@RestController
@Api(description = "用户cart订单")
@RequestMapping("/api/order")
public class CartOrderController {
    @Autowired
    private CartOrderService cartOrderService;

    @PostMapping(value = "/list")
    @ApiOperation(value = "list",notes = "list")
    public Res list(@RequestBody CartOrderListVo cartOrderListVo){
        Res res = null;
        try {
            if (null!=cartOrderListVo.getSeldate()&&cartOrderListVo.getSeldate().length>0){
                cartOrderListVo.setStartTime(cartOrderListVo.getSeldate()[0]);
                cartOrderListVo.setEndTime(cartOrderListVo.getSeldate()[1]);
            }
            PageInfo<CartOrderListDto> list = cartOrderService.list(cartOrderListVo);
            res = new Res(list);
        }catch (Exception e){
            e.printStackTrace();
            res = new Res(Res.CODE_FAIL,e.getMessage());
        }
        return res;
    }

    @PostMapping(value = "/update")
    @ApiOperation(value = "updata",notes = "updata")
    public Res update(@RequestBody CartOrderUpdateVo cartOrderUpdateVo){
        Res res = null;
        try {
            int i = cartOrderService.update(cartOrderUpdateVo);
            res = new Res(i);
        }catch (Exception e){
            e.printStackTrace();
            res = new Res(Res.CODE_FAIL,e.getMessage());
        }
        return res;
    }

    @PostMapping(value = "/add")
    @ApiOperation(value = "add",notes = "add")
    public Res add(@RequestBody CartOrderAddVo cartOrderAddVo){
        Res res = null;
        try {
            int i = cartOrderService.add(cartOrderAddVo);
            res = new Res(i);
        }catch (Exception e){
            e.printStackTrace();
            res = new Res(Res.CODE_FAIL,e.getMessage());
        }
        return res;
    }
}
